package com.supreme.threadserver.service;

import com.supreme.threadserver.threadpkg.AtomicTask;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicLong;

@Service
public class AtomicService {

    public void testAtomicTaskThread() {
        AtomicLong atomicLong = new AtomicLong(0);

        FutureTask<Long> futureTask1 = new FutureTask<>(new AtomicTask(10, atomicLong));
        new Thread(futureTask1).start();

        FutureTask<Long> futureTask2 = new FutureTask<>(new AtomicTask(10, atomicLong));
        new Thread(futureTask2).start();

        FutureTask<Long> futureTask3 = new FutureTask<>(new AtomicTask(10, atomicLong));
        new Thread(futureTask3).start();

        FutureTask<Long> futureTask4 = new FutureTask<>(new AtomicTask(10, atomicLong));
        new Thread(futureTask4).start();

        try {
            Long result1 = futureTask1.get();
            Long result2 = futureTask2.get();
            Long result3 = futureTask3.get();
            Long result4 = futureTask4.get();
            System.out.println("atomicLong.longValue() = " + atomicLong.longValue());
        } catch (InterruptedException | ExecutionException e) {
            throw new RuntimeException(e);
        }
    }
}
